using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using XnaGame = Microsoft.Xna.Framework.Game;

namespace GT.Xna.Windows
{
    public static partial class GameFunctions
    {
        public static Vector2 GridToPixel(Texture2D texture)
        {
            return GridToPixel(texture, GameGlobals.DefaultSettings.StartPosition.To2D());
        }

        public static Vector2 GridToPixel(Texture2D texture, Vector2 location)
        {
            Vector2 ret = location;
            ret.X *= texture.Width;
            ret.Y *= texture.Height;
            return ret;
        }

        public static Vector2 PixelToGrid(Texture2D texture)
        {
            return PixelToGrid(texture, GameGlobals.DefaultSettings.StartPosition.To2D());
        }

        public static Vector2 PixelToGrid(Texture2D texture, Vector2 location)
        {
            Vector2 ret = location;
            ret.X *= (1 / texture.Width);
            ret.Y *= (1 / texture.Height);
            return ret;
        }

        public static Vector2 GetCenter(Texture2D texture)
        {
            return new Vector2(texture.Width * 0.5f, texture.Height * 0.5f);
        }

        public static Vector3 GetCenter(Texture3D texture)
        {
            return new Vector3(texture.Width * 0.5f, texture.Height * 0.5f, texture.Depth * 0.5f);
        }
    }

    public static partial class GameGlobals
    {
        static GameGlobals()
        {
            DefaultSettings = new Settings();            
            DefaultSpriteBatch = null;
            Game = null;
            Origin = Vector3.Zero;
            PositionOffset = PositionOffsetMode.Normal;
            TextureCache = new List<Texture2D>();
        }

        public static Settings DefaultSettings { get; private set; }
        public static SpriteBatch DefaultSpriteBatch { get; set; }
        public static XnaGame Game { get; set; }
        public static Vector3 Origin { get; set; }
        public static PositionOffsetMode PositionOffset { get; set; }
        public static List<Texture2D> TextureCache { get; set; }
    }

    public class Settings
    {
        internal Settings()
        {
            ScaleMode = PositionScaleMode.Pixel;
            StartPosition = Vector3.Zero;
        }

        public PositionScaleMode ScaleMode { get; set; }
        public Vector3 StartPosition { get; set; }
    }

    public enum PositionScaleMode : int
    {
        Pixel = 0,
        ObjectSize = 1
    }

    public enum PositionOffsetMode : int
    {
        Normal = 0,
        CenterObject = 1
    }
}
